package com.google.gwt.sample.contacts.server.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gwt.sample.contacts.shared.Contact;
import com.google.gwt.sample.contacts.shared.ContactDetails;

@Path("/contacts")
public class ContactsResource {

	private final static HashMap<String, Contact> contacts = new HashMap<String, Contact>();

	static {
		final String[] contactsFirstNameData = new String[] { "Hollie",
				"Emerson", "Healy", "Brigitte", "Elba", "Claudio", "Dena",
				"Christina", "Gail", "Orville", "Rae", "Mildred", "Candice",
				"Louise", "Emilio", "Geneva", "Heriberto", "Bulrush",
				"Abigail", "Chad", "Terry", "Bell" };

		final String[] contactsLastNameData = new String[] { "Voss", "Milton",
				"Colette", "Cobb", "Lockhart", "Engle", "Pacheco", "Blake",
				"Horton", "Daniel", "Childers", "Starnes", "Carson",
				"Kelchner", "Hutchinson", "Underwood", "Rush", "Bouchard",
				"Louis", "Andrews", "English", "Snedden" };

		final String[] contactsEmailData = new String[] { "mark@example.com",
				"hollie@example.com", "boticario@example.com",
				"emerson@example.com", "healy@example.com",
				"brigitte@example.com", "elba@example.com",
				"claudio@example.com", "dena@example.com",
				"brasilsp@example.com", "parker@example.com",
				"derbvktqsr@example.com", "qetlyxxogg@example.com",
				"antenas_sul@example.com", "cblake@example.com",
				"gailh@example.com", "orville@example.com",
				"post_master@example.com", "rchilders@example.com",
				"buster@example.com", "user31065@example.com",
				"ftsgeolbx@example.com" };

		for (int i = 0; i < contactsFirstNameData.length
				&& i < contactsLastNameData.length
				&& i < contactsEmailData.length; ++i) {
			Contact contact = new Contact(String.valueOf(i),
					contactsFirstNameData[i], contactsLastNameData[i],
					contactsEmailData[i]);
			contacts.put(contact.getId(), contact);
		}
	}

	public ContactsResource() {
	}

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Contact addContact(Contact contact) {
		contact.setId(String.valueOf(contacts.size()));
		contacts.put(contact.getId(), contact);
		return contact;
	}

	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Contact updateContact(Contact contact) {
		contacts.remove(contact.getId());
		contacts.put(contact.getId(), contact);
		return contact;
	}

	public Boolean deleteContact(@PathParam("id") String id) {
		contacts.remove(id);
		return true;
	}

	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	public List<ContactDetails> deleteContacts(List<String> ids) {
		for (int i = 0; i < ids.size(); ++i) {
			deleteContact(ids.get(i));
		}

		return getContactDetails();
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<ContactDetails> getContactDetails() {
		ArrayList<ContactDetails> contactDetails = new ArrayList<ContactDetails>();

		Iterator<String> it = contacts.keySet().iterator();
		while (it.hasNext()) {
			Contact contact = contacts.get(it.next());
			contactDetails.add(contact.getLightWeightContact());
		}

		return contactDetails;
	}

	@Path("/{id}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Contact getContact(@PathParam("id") String id) {
		return contacts.get(id);
	}
}
